<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>用户添加</title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
    <link media="all" rel="stylesheet" th:href="@{/libs/layui-v2.5.5/css/layui.css}">
    <link media="all" rel="stylesheet" th:href="@{/css/public.css}">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <input name="uIds" th:value="${uIds}" type="hidden">
    <input id="parentId" name="dId" type="hidden">
    <div class="layui-form-item">
        <label class="layui-form-label">绑定部门</label>
        <div class="layui-input-inline">
            <input autocomplete="off" class="layui-input" id="tree" name=""
                   placeholder="请输入" type="text">
        </div>
        <div class="layui-form-mid layui-word-aux"></div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-filter="saveBtn" lay-submit>确认保存</button>
        </div>
    </div>
</div>
<script charset="utf-8" th:src="@{/libs/layui-v2.5.5/layui.js}"></script>
<script charset="utf-8" th:src="@{/js/lay-config.js}"></script>
<script th:inline="none">
    layui.use(['form', 'laydate', 'tableSelect'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$,
            tableSelect = layui.tableSelect;

        tableSelect.render({
            elem: '#tree',
            checkedKey: 'id',
            height: '400',
            width: '900',
            searchType: 'more',
            searchList: [
                {searchKey: 'name', searchPlaceholder: '部门名'},
            ],
            table: {
                response: {
                    statusCode: 200
                },
                url: '/system/depart/listPage',
                cols: [[
                    {type: 'radio'},
                    {field: 'id', title: 'ID'},
                    {field: 'name', minWidth: 200, title: '部门名'},
                    {field: 'idx', align: 'center', title: '排序号'},
                    {field: 'createBy', align: 'center', title: '创建人'},
                    {field: 'createTime', align: 'center', title: '创建时间'},
                    {field: 'remark', align: 'center', title: '备注'},
                ]]
            },
            done: function (elem, data) {
                var NEWJSON = []
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.name)
                    $("#parentId").val(item.id)
                })
                elem.val(NEWJSON.join(","))
            }
        })

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert("是否确认提交修改!", {
                title: '最终的提交信息'
            }, function () {
                $.ajax({
                    type: "POST",
                    url: "/system/depart/binding",
                    data: {
                        uIds: data.field.uIds,
                        dId: data.field.dId,
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data.code === 200) {
                            // 关闭弹出层
                            layer.alert(data.message)
                            layer.close(index);
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        } else {
                            layer.alert(data.message)
                        }
                    }
                });
            });
            return false;
        });
    });
</script>
</body>
</html>